define(["angularAMD"], function (angularAMD) {
    angularAMD.service('QcItemEditService', ['$state','$rootScope', '$filter', '$stateParams', 'config', '$mdDialog', 'MMWService',
        function ($state, $rootScope, $filter, $stateParams, config, $mdDialog, $MMWService) {
            this.init = function(options){
                openDialog(options);
            };

            function openDialog(options){
                $mdDialog.dialog(
                    'module/modulesSPC/program/B0504/qcItemEdit.tmp.html',
                    function(dialog){
                        return {
                            css_url:'module/modulesSPC/program/B0504/qcItemEdit.css',
                            filterItem: {queryUnselect:''},  //未選擇區的查詢條件
                            typeList: [],       //未選擇區的第一層選項
                            selectList: angular.copy(options.selectList) || [], //已選清單
                            typeItem: {},       //目前未選擇區選擇的第一層選項
                            selectMap: {},      //用來紀錄各層選項已選的子項目
                            typeCountMap: {},   //用來紀錄各層選項已選的子項目數量加總
                            beforeShown: function(){
                                genSelectMap(dialog);
                                dialog.search();
                            },
                            clickTypeItem: function(index, item){
                                dialog.typeItem = item;
                                dialog.typeItem.showMachines = !dialog.typeItem.showMachines;
                            },
                            clickUnselectItem: function(index, item){
                                dialog.selectList.push({qc_item_no: item.qc_item_no, qc_item_name: item.qc_item_name, qc_type: item.qc_type});
                                dialog.selectMap[item.qc_type + item.qc_item_no] = true;
                                if(!dialog.typeCountMap[item.qc_type]){
                                    dialog.typeCountMap[item.qc_type] = 0;
                                }
                                dialog.typeCountMap[item.qc_type]++;
                            },
                            clickselectItem: function(index, item){
                                dialog.selectList.splice(index, 1);
                                delete dialog.selectMap[item.qc_type + item.qc_item_no];
                                dialog.typeCountMap[item.qc_type]--;
                            },
                            filterUnselect: function(item){
                                return (dialog.filterItem.queryUnselect =='' || item.qc_item_no.indexOf(dialog.filterItem.queryUnselect) !=-1) && !dialog.selectMap[item.qc_type + item.qc_item_no];
                            },
                            search: function(){
                                qcItemInfoGet(options, function(data){
                                    var typeList = [ //0：計數  1：計量
                                        {qc_type: 0, qcItemList:[]},
                                        {qc_type: 1, qcItemList:[]}
                                    ];
                                    if(data.qc_item_a_detail && data.qc_item_a_detail.length > 0){
                                        typeList[0].qcItemList = genQcItemList(data.qc_item_a_detail, 0);
                                    }
                                    if(data.qc_item_v_detail && data.qc_item_v_detail.length > 0){
                                        typeList[1].qcItemList = genQcItemList(data.qc_item_v_detail, 1);
                                    }
                                    dialog.typeList = typeList;
                                    dialog.typeItem = typeList[0];
                                    if(dialog.typeItem.qcItemList.length > 0){
                                        dialog.typeItem.showMachines = true;
                                    }
                                    genTypeCountMap(dialog);
                                });
                            },
                            confirm : function(){
                                if(options.confirm){
                                    options.confirm(dialog);
                                } else {
                                    dialog.hide();
                                }
                            },
                            back : function(){
                                if(options.close){
                                    options.close(dialog);
                                } else {
                                    dialog.hide();
                                }
                            }
                        }
                    }
                )
            };            

            function genQcItemList(detail, qc_type){
                detail.forEach(function(item){
                    item.qc_type = qc_type;
                });
                return detail;
            }

            function genSelectMap(dialog){
                dialog.selectMap = {};
                if(dialog.selectList && dialog.selectList.length>0){
                    dialog.selectList.forEach(function(selectData){
                        dialog.selectMap[selectData.qc_type + selectData.qc_item_no] = true;
                        if(!dialog.typeCountMap[selectData.qc_type]){
                            dialog.typeCountMap[selectData.qc_type] = 1;
                        }else{
                            dialog.typeCountMap[selectData.qc_type]++;
                        }
                    });
                }
            }

            //20180417 modify by Dustdusk for mantis#0045371:
            function genTypeCountMap(dialog){
                if(dialog.typeList && dialog.typeList.length >0){
                    dialog.typeList.forEach(function(typeData){
                        if(!dialog.typeCountMap[typeData.qc_type]){
                            dialog.typeCountMap[typeData.qc_type] = 0;
                        }
                    });
                }
            }

            //品管項目資訊取得
            function qcItemInfoGet(options, callback){
                // var data = {
                //     qc_item_v_detail: [
                //         {qc_item_no: '001', qc_item_name: '計量項目001'},
                //         {qc_item_no: '002', qc_item_name: '計量項目002'},
                //         {qc_item_no: '003', qc_item_name: '計量項目003'},
                //         {qc_item_no: '004', qc_item_name: '計量項目004'},
                //         {qc_item_no: '005', qc_item_name: '計量項目005'},
                //         {qc_item_no: '006', qc_item_name: '計量項目006'},
                //         {qc_item_no: '007', qc_item_name: '計量項目007'},
                //         {qc_item_no: '008', qc_item_name: '計量項目008'},
                //         {qc_item_no: '009', qc_item_name: '計量項目009'},
                //         {qc_item_no: '010', qc_item_name: '計量項目010'}
                //     ],
                //     qc_item_a_detail: [
                //         {qc_item_no: '001', qc_item_name: '計數項目001'},
                //         {qc_item_no: '002', qc_item_name: '計數項目002'},
                //         {qc_item_no: '003', qc_item_name: '計數項目003'},
                //         {qc_item_no: '004', qc_item_name: '計數項目004'},
                //         {qc_item_no: '005', qc_item_name: '計數項目005'},
                //         {qc_item_no: '006', qc_item_name: '計數項目006'},
                //         {qc_item_no: '007', qc_item_name: '計數項目007'},
                //         {qc_item_no: '008', qc_item_name: '計數項目008'},
                //         {qc_item_no: '009', qc_item_name: '計數項目009'},
                //         {qc_item_no: '010', qc_item_name: '計數項目010'}
                //     ]
                // }
                // if(callback)
                //     callback(data);
                var content = {
                    item_type: options.workItem.item_type || '',
                    item_no: options.workItem.item_no || '',
                    op_no: options.workItem.op_no || '',
                    inspect_type: options.workItem.inspect_type || ''
                };
                console.log("品管項目資訊取得(qc_item_info_get) send:", content);
                $MMWService.sendToSMESServer({
                    uri: 'tcSPC.clsSpc.qc_item_info_get',
                    content : content,
                    success : function(data){
                        console.log("品管項目資訊取得(qc_item_info_get) return:", data);
                        if(callback)
                            callback(data);
                    }
                });
            }
        }
    ])
});
